<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class AnnouncementsController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Announcements';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form', 'Javascript');

    /**
     * To set a default sort order
     *
     */
    var $paginate = array('order'=>array('Announcement.created' => 'desc'));


    /**
     * Describe index
     *
     * @param $committee
     * @return null
     */
    function index($committee) {
        $this->Announcement->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Announcement->recursive = 0;
        $announcement=$this->paginate('Announcement',array('committee_id'=>$this->dcommittee['Committee']['id']));
        $this->set('announcements', $announcement);
    }

    /**
     * Describe view
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function view($committee,$id = null) {
        $this->Announcement->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);

        $this->set('dcommittee',$this->dcommittee);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Announcement.', true));
            $this->redirect(array('committee'=>$this->dcommittee['Committee']['short_name'],'action'=>'index'));
        }
        $this->set('announcement', $this->Announcement->read(null, $id));
    }

    /**
     * Describe add
     *
     * @param $committee
     * @return null
     */
    function add($committee) {
        $this->Announcement->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        
        $this->set('dcommittee',$this->dcommittee);
        if (!empty($this->data)) {
            $this->data['Announcement']['committee_id']=$this->dcommittee['Committee']['id'];
            $this->Announcement->create();
            $this->data['Announcement']['user_id']=$this->Auth->user('id');
            if ($this->Announcement->save($this->data)) {
                $this->Session->setFlash(__('The Announcement has been saved', true));
                $this->redirect(array('action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
            } else {
                $this->Session->setFlash(__('The Announcement could not be saved. Please, try again.', true));
            }
        }
        $users = $this->Announcement->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']);
        $this->set(compact('users'));
    }

    /**
     * Describe edit
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function edit($committee,$id = null) {
        $this->Announcement->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
       
        $this->set('dcommittee',$this->dcommittee);
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Announcement', true));
            $this->redirect(array('action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
        }
        if (!empty($this->data)) {
            if ($this->Announcement->save($this->data)) {
                $this->Session->setFlash(__('The Announcement has been saved', true));
                $this->redirect(array('action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
            } else {
                $this->Session->setFlash(__('The Announcement could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Announcement->read(null, $id);
        }
        $users = $this->Announcement->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']);
        $this->set(compact('users'));
    }

    /**
     * Describe delete
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function delete($committee,$id = null) {
        $this->dcommittee=$this->Announcement->Committee->findByShortName($committee);
        $this->set('dcommittee',$this->dcommittee);
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Announcement', true));
            $this->redirect(array('action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
        }
        $this->Announcement->delete($id);
        $this->Session->setFlash(__('Announcement deleted', true));
        $this->redirect(array('action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
    }
}
